package community.hospital.service.domain.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import community.hospital.entity.Role;
import community.hospital.entity.request.ListRoleRequest;
import community.hospital.service.domain.RoleService;
import community.hospital.mapper.RoleMapper;
import org.springframework.stereotype.Service;

import java.util.List;

import static community.hospital.common.Constant.STATE_ENABLE;

/**
 * @author ydj
 * @description 针对表【role】的数据库操作Service实现
 * @createDate 2023-01-16 16:35:35
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
        implements RoleService {

    @Override
    public int countByRoleNameAndRoleCode(String roleCode, String roleName) {
        return this.lambdaQuery()
                .eq(Role::getRoleCode, roleCode)
                .or()
                .eq(Role::getRoleName, roleName)
                .count()
                .intValue();
    }

    @Override
    public Page<Role> listRole(ListRoleRequest request) {
        Page<Role> page = new Page<>(request.getPageNumber(), request.getPageSize());
        String roleName = request.getRoleName();
        String roleCode = request.getRoleCode();
        return lambdaQuery()
                .eq(roleName != null, Role::getRoleName, roleName)
                .eq(roleCode != null, Role::getRoleCode, roleCode)
                .page(page);
    }

    @Override
    public List<Role> listAllRole() {
        return lambdaQuery()
                .eq(Role::getState, STATE_ENABLE)
                .list();
    }
}




